﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Lswweb.Model;
using Lswweb.IDAL;

namespace Lswweb.SqlServerDAL
{
    public class GuestBookDAL : IGuestBook
    {
        #region 成员字段及构造方法

        private Builder<GuestbookInfo> guestBookBuilder = new Builder<GuestbookInfo>("GuestBook","Id");

        public GuestBookDAL()
        {
            guestBookBuilder.CreateModel += new ReaderBind<GuestbookInfo>(guestBookBuilder_CreateModel);
            guestBookBuilder.GetParameters += new ParametersBuilder<GuestbookInfo>(guestBookBuilder_GetParameters);
        }

        IDataParameter[] guestBookBuilder_GetParameters(GuestbookInfo t)
        {
            return new IDataParameter[]{
                DataAccess.CreateParameter("Id",t.Id),
                DataAccess.CreateParameter("ItemType",(int)t.ItemType),
                DataAccess.CreateParameter("Title",t.Title),
                DataAccess.CreateParameter("LinkMan", t.LinkMan),
                DataAccess.CreateParameter("Tel", t.Tel),
                DataAccess.CreateParameter("Mobile", t.Mobile),
                DataAccess.CreateParameter("Fax", t.Fax),
                DataAccess.CreateParameter("Email", t.Email),
                DataAccess.CreateParameter("Content", t.Content),
                DataAccess.CreateParameter("IsRead",t.IsRead),
                DataAccess.CreateParameter("DateCreated",t.DateCreated),
                DataAccess.CreateParameter("Sex",(int)t.Sex),
                DataAccess.CreateParameter("Address",t.Address)};
        }

        GuestbookInfo guestBookBuilder_CreateModel(Hashtable reader)
        {
            GuestbookInfo guestBook = new GuestbookInfo();

            if (reader.ContainsKey("Id"))
                guestBook.Id = Convert.ToInt32(reader["Id"]);

            if (reader.ContainsKey("ItemType"))
                guestBook.ItemType = (GuestBookItem)Convert.ToInt32(reader["ItemType"]);

            if (reader.ContainsKey("Content"))
                guestBook.Content = reader["Content"].ToString();

            if (reader.ContainsKey("Email"))
                guestBook.Email = reader["Email"].ToString();

            if (reader.ContainsKey("Fax"))
                guestBook.Fax = reader["Fax"].ToString();

            if (reader.ContainsKey("IsRead"))
                guestBook.IsRead = Convert.ToBoolean(reader["IsRead"]);

            if (reader.ContainsKey("LinkMan"))
                guestBook.LinkMan = reader["LinkMan"].ToString();

            if (reader.ContainsKey("Mobile"))
                guestBook.Mobile = reader["Mobile"].ToString();

            if (reader.ContainsKey("Tel"))
                guestBook.Tel = reader["Tel"].ToString();

            if (reader.ContainsKey("Title"))
                guestBook.Title = reader["Title"].ToString();

            if (reader.ContainsKey("DateCreated"))
                guestBook.DateCreated = Convert.ToDateTime(reader["DateCreated"]);

            if (reader.ContainsKey("Sex"))
                guestBook.Sex = (SexType)Convert.ToInt32(reader["Sex"]);

            if (reader.ContainsKey("Address"))
                guestBook.Address = reader["Address"].ToString();

            return guestBook;
        }

        #endregion

        #region IGuestBook 成员

        public GuestbookInfo GetGuestBook(int id)
        {
            if (id <= 0)
                return null;

            return guestBookBuilder.GetModel(id);
        }

        public IList<GuestbookInfo> GetGuestBooks(bool? isRead,GuestBookItem? itemType, int pageSize, int pageIndex, out int totalRecord)
        {
            string where = isRead == null
                ? string.Empty
                : "IsRead=@IsRead";
            
            if (itemType != null)
                where += " and ItemType=@ItemType";

            return guestBookBuilder.GetModels(string.Empty, where, "id desc", pageIndex, pageSize, out totalRecord, DataAccess.CreateParameter("IsRead",isRead),DataAccess.CreateParameter("ItemType",itemType));
        }

        public int Insert(GuestbookInfo guestBook)
        {
            if (guestBook == null)
                return -1;

            return guestBookBuilder.Insert(
                guestBook,
                "Title",
                "ItemType",
                "LinkMan",
                "Tel",
                "Mobile",
                "Fax",
                "Email",
                "Content",
                "Sex",
                "Address");
        }

        public bool Delete(int id)
        {
            if (id <= 0)
                return false;

            return guestBookBuilder.Delete(id);
        }

        public bool UpdateIsRead(int id)
        {
            if (id <= 0)
                return false;

            return guestBookBuilder.UpdateFields("IsRead",id,true);
        }

        #endregion
    }
}
